package com.th.supcom.pds.drugsupply.client.mvc;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.carefx.fc.component.editor.client.EMREditorPanel;
import net.carefx.framework.container.client.page.PageContext;

import com.extjs.gxt.ui.client.Style.HideMode;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.th.supcom.framework.print.client.DynamicPrintModel;
import com.th.supcom.framework.print.client.GridDataSetModel;
import com.th.supcom.framework.print.client.GridRowModel;
import com.th.supcom.framework.print.client.PrintUtils;
import com.th.supcom.pds.drugsupply.client.common.PdsSplitInfoModuleEvents;
import com.th.supcom.pds.drugsupply.client.model.PdsPackageInfoModel;
import com.th.supcom.pds.drugsupply.client.widget.PdsSplitInfoMainPanel;

/**
 * PhamSplitInfo视图.
 * 
 * 药品信息.
 */
public class PdsSplitInfoView extends View
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private EMREditorPanel m_editorPanel;

    private PdsSplitInfoMainPanel m_pdsSplitInfoMainPanel;

    /**
     * @param controller
     */
    public PdsSplitInfoView (final Controller controller)
    {
        super (controller);
    }

    private String avoidNull (final Object str)
    {
        return str == null ? "　 " : str + "";
    }

    @Override
    protected void handleEvent (final AppEvent event)
    {
        if (PdsSplitInfoModuleEvents.UI_INIT.equals (event.getType ()))
        {
            final PageContext context = event.getData ();
            this.initUI (context);
        }
        else if (PdsSplitInfoModuleEvents.QUERY_INFO.equals (event.getType ()))
        {
            m_pdsSplitInfoMainPanel.queryInfo ();
        }
        else if (PdsSplitInfoModuleEvents.ADD_DRUG_INFO.equals (event.getType ()))
        {
            m_pdsSplitInfoMainPanel.addInfo ();
        }
        else if (PdsSplitInfoModuleEvents.SAVE_INFO.equals (event.getType ()))
        {
            m_pdsSplitInfoMainPanel.modifyInfo ();
        }
        else if (PdsSplitInfoModuleEvents.REFRESH_DRUG_INFO.equals (event.getType ()))
        {
            m_pdsSplitInfoMainPanel.refreshInfo ();
        }
        else if (PdsSplitInfoModuleEvents.DELETE_DRUG_INFO.equals (event.getType ()))
        {
            m_pdsSplitInfoMainPanel.deleteInfo ();
        }
        else if (PdsSplitInfoModuleEvents.PRINT_DRUG_INFO.equals (event.getType ()))
        {
            onPrintInfo ();
            return;
        }

    }

    /**
     * 初始化容器面板.
     * 
     * @param context
     */
    private void initUI (final PageContext context)
    {
        /** 主窗口容器. */
        LayoutContainer mainLayoutContainer;
        mainLayoutContainer = context.getContainer ();
        mainLayoutContainer.setLayout (new FitLayout ());
        mainLayoutContainer.setHideMode (HideMode.OFFSETS);
        m_pdsSplitInfoMainPanel = new PdsSplitInfoMainPanel ();
        m_editorPanel = PrintUtils.createReportPanel ();
        mainLayoutContainer.add (m_pdsSplitInfoMainPanel);
        mainLayoutContainer.add (m_editorPanel);
    }

    private void onPrintInfo ()
    { // 得到要打印的数据
        final List <PdsPackageInfoModel> pdsPackageInfoModels = m_pdsSplitInfoMainPanel.getGridListModels ();
        printPackageInfo (pdsPackageInfoModels, "请选择要打印的药品信息");

    }

    private void printPackageInfo (final List <PdsPackageInfoModel> pdsPackageInfoModels, final String msg)
    {
        if (pdsPackageInfoModels == null || pdsPackageInfoModels.isEmpty ())
        {
            MessageBox.alert ("警告", msg, null);
            return;
        }
        final DynamicPrintModel dynamicPrintModel = new DynamicPrintModel ();
        final List <GridDataSetModel> gridDataList = new ArrayList <GridDataSetModel> ();
        GridDataSetModel gridDataSetModel = new GridDataSetModel ("Grid");//表名 
        for (final PdsPackageInfoModel pdsPackageInfoModel : pdsPackageInfoModels)
        {
            final List <String> attributeList = new ArrayList <String> ();
            attributeList.add (avoidNull (pdsPackageInfoModel.getPhamName ()));
            attributeList.add (avoidNull (pdsPackageInfoModel.getPhamSpec ()));
            attributeList.add (avoidNull (pdsPackageInfoModel.getPackageUnitName ()));
            attributeList.add (avoidNull (pdsPackageInfoModel.getDosePerUnit ()) + avoidNull (pdsPackageInfoModel.getDoseUnitName ()));
            attributeList.add (avoidNull (pdsPackageInfoModel.getPackageFactor ()) + avoidNull (pdsPackageInfoModel.getPhamUnitName ()) + "/"
                               + avoidNull (pdsPackageInfoModel.getPackageUnitName ()));
            GridRowModel gridRowModel = new GridRowModel ();
            gridRowModel.setColumnValues (attributeList);
            gridDataSetModel.addAttributeValue (gridRowModel);
        }
        gridDataList.add (gridDataSetModel);
        final Map <String, String> keyValueMap = new HashMap <String, String> ();
        final String date = DateTimeFormat.getFormat ("yyyyMMdd HH:ss").format (new Date ());
        keyValueMap.put ("year", date.substring (0, 4));
        keyValueMap.put ("month", date.substring (4, 6));
        keyValueMap.put ("day", date.substring (6, 8));
        keyValueMap.put ("Report--Layout--Grid::rowCount", "31");

        keyValueMap.put ("Report--DataSets--DataSet_Grid::rowCount", pdsPackageInfoModels.size () + "");

        dynamicPrintModel.setPath ("drugPackageInfo.xml");
        dynamicPrintModel.setKeyValueMap (keyValueMap);//静态数据
        dynamicPrintModel.setGridDataList (gridDataList);//动态数据
        PrintUtils.printDynamicReport (m_editorPanel, dynamicPrintModel, null);
    }
}